/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dal_cyclonedds/converter/StatusMaskConverter.hpp"

StatusMaskConverter* StatusMaskConverter::singleton_ = nullptr;

StatusMaskConverter::StatusMaskConverter() {}

StatusMaskConverter* StatusMaskConverter::GetInstance()
{
    if (singleton_ == nullptr) {
        singleton_ = new StatusMaskConverter();
    }
    return singleton_;
}

void StatusMaskConverter::ConvertStatusMaskFromDalToCyclonedds(dal_status_mask_t d_mask,
                                                               uint32_t* c_mask)
{
    *c_mask = 0;
    if (d_mask & DAL_INCONSISTENT_TOPIC_STATUS) {
        *c_mask |= DDS_INCONSISTENT_TOPIC_STATUS;
    }
    if (d_mask & DAL_OFFERED_DEADLINE_MISSED_STATUS) {
        *c_mask |= DDS_OFFERED_DEADLINE_MISSED_STATUS;
    }
    if (d_mask & DAL_REQUESTED_DEADLINE_MISSED_STATUS) {
        *c_mask |= DDS_REQUESTED_DEADLINE_MISSED_STATUS;
    }
    if (d_mask & DAL_OFFERED_INCOMPATIBLE_QOS_STATUS) {
        *c_mask |= DDS_OFFERED_INCOMPATIBLE_QOS_STATUS;
    }
    if (d_mask & DAL_REQUESTED_INCOMPATIBLE_QOS_STATUS) {
        *c_mask |= DDS_REQUESTED_INCOMPATIBLE_QOS_STATUS;
    }
    if (d_mask & DAL_SAMPLE_LOST_STATUS) {
        *c_mask |= DDS_SAMPLE_LOST_STATUS;
    }
    if (d_mask & DAL_SAMPLE_REJECTED_STATUS) {
        *c_mask |= DDS_SAMPLE_REJECTED_STATUS;
    }
    if (d_mask & DAL_DATA_ON_READERS_STATUS) {
        *c_mask |= DDS_DATA_ON_READERS_STATUS;
    }
    if (d_mask & DAL_DATA_AVAILABLE_STATUS) {
        *c_mask |= DDS_DATA_AVAILABLE_STATUS;
    }
    if (d_mask & DAL_LIVELINESS_LOST_STATUS) {
        *c_mask |= DDS_LIVELINESS_LOST_STATUS;
    }
    if (d_mask & DAL_LIVELINESS_CHANGED_STATUS) {
        *c_mask |= DDS_LIVELINESS_CHANGED_STATUS;
    }
    if (d_mask & DAL_PUBLICATION_MATCHED_STATUS) {
        *c_mask |= DDS_PUBLICATION_MATCHED_STATUS;
    }
    if (d_mask & DAL_SUBSCRIPTION_MATCHED_STATUS) {
        *c_mask |= DDS_SUBSCRIPTION_MATCHED_STATUS;
    }
}

void StatusMaskConverter::FilterDomainParticipantStatusMask(uint32_t* c_mask)
{
    *c_mask &= CYCLONE_DOMAIN_PARTICIPANT_STATUS_MASK;
}

void StatusMaskConverter::FilterPublisherStatusMask(uint32_t* c_mask)
{
    *c_mask &= CYCLONE_PUBLISHER_STATUS_MASK;
}

void StatusMaskConverter::FilterSubscriberStatusMask(uint32_t* c_mask)
{
    *c_mask &= CYCLONE_SUBSCRIBER_STATUS_MASK;
}

void StatusMaskConverter::FilterTopicStatusMask(uint32_t* c_mask)
{
    *c_mask &= CYCLONE_TOPIC_STATUS_MASK;
}

void StatusMaskConverter::FilterDataWriterStatusMask(uint32_t* c_mask)
{
    *c_mask &= CYCLONE_WRITER_STATUS_MASK;
}

void StatusMaskConverter::FilterDataReaderStatusMask(uint32_t* c_mask)
{
    *c_mask &= CYCLONE_READER_STATUS_MASK;
}